package com.junyuan.fxf_pad.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.junyuan.fxf_pad.R;
import com.junyuan.fxf_pad.activity.PlayVoiceActivity;
import com.junyuan.fxf_pad.application.MyApplication;

import java.util.ArrayList;

public class VoiceFragment extends Fragment implements View.OnClickListener {
    private LinearLayout llVoiceLeft, llVoiceRight;
    private ImageButton btn_voice_0_2, btn_voice_3_4, btn_voice_5_6, btn_voice_7_8, btn_voice_9_10, btn_voice_11_12;

    ArrayList<String> mVoiceList = new ArrayList<String>();
    Intent intent;
    Bundle bundle;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            onResume();
        }else {
            return;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_voice, null);
        initViews(view);

        return view;

    }

    @Override
    public void onClick(View view) {
        if (MyApplication.instance.mBleService != null) {
            MyApplication.instance.mBleService.writeCharacteristic
                    (MyApplication.instance.mCharacteristic, "E");
        }
        switch (view.getId()) {
            case R.id.btn_voice_0_2:
                if (MyApplication.instance.mBleService != null) {

                    try {
                        Thread.sleep(100);
                        MyApplication.instance.mBleService.writeCharacteristic
                                (MyApplication.instance.mCharacteristic, "#C");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                break;
            case R.id.btn_voice_3_4:
                if (MyApplication.instance.mBleService != null) {
                    try {
                        Thread.sleep(100);

                        MyApplication.instance.mBleService.writeCharacteristic
                                (MyApplication.instance.mCharacteristic, "#D");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                break;
            case R.id.btn_voice_5_6:
                try {
                    Thread.sleep(100);
                    if (MyApplication.instance.mBleService != null) {
                        MyApplication.instance.mBleService.writeCharacteristic
                                (MyApplication.instance.mCharacteristic, "#E");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_voice_7_8:
                if (MyApplication.instance.mBleService != null) {
                    try {
                        Thread.sleep(100);
                        MyApplication.instance.mBleService.writeCharacteristic
                                (MyApplication.instance.mCharacteristic, "#F");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_voice_9_10:
                if (MyApplication.instance.mBleService != null) {
                    MyApplication.instance.mBleService.writeCharacteristic
                            (MyApplication.instance.mCharacteristic, "#G");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_voice_11_12:
                if (MyApplication.instance.mBleService != null) {
                    MyApplication.instance.mBleService.writeCharacteristic
                            (MyApplication.instance.mCharacteristic, "#G");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;

        }
        mVoiceList = getMusicData(view.getId());
        intent = new Intent(getContext(), PlayVoiceActivity.class);
        bundle = new Bundle();
        bundle.putStringArrayList("VoiceList", mVoiceList);
        intent.putExtra("Bundle", bundle);
        startActivity(intent);
    }


    @Override
    public void onResume() {
        super.onResume();
        setAnimation();
    }

    private void setAnimation() {
        AnimationSet animationSet = new AnimationSet(true);
        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        translateAnimation.setDuration(1000);
        animationSet.addAnimation(translateAnimation);
        llVoiceLeft.startAnimation(translateAnimation);

        AnimationSet animationSet2 = new AnimationSet(true);
        TranslateAnimation translateAnimation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        translateAnimation2.setDuration(1000);
        animationSet2.addAnimation(translateAnimation2);
        llVoiceRight.startAnimation(translateAnimation2);
    }

    private void initViews(View view) {
        llVoiceLeft = (LinearLayout) view.findViewById(R.id.llVoiceLeft);
        llVoiceRight = (LinearLayout) view.findViewById(R.id.llVoiceRight);
        btn_voice_0_2 = (ImageButton) view.findViewById(R.id.btn_voice_0_2);
        btn_voice_3_4 = (ImageButton) view.findViewById(R.id.btn_voice_3_4);
        btn_voice_5_6 = (ImageButton) view.findViewById(R.id.btn_voice_5_6);
        btn_voice_7_8 = (ImageButton) view.findViewById(R.id.btn_voice_7_8);
        btn_voice_9_10 = (ImageButton) view.findViewById(R.id.btn_voice_9_10);
        btn_voice_11_12 = (ImageButton) view.findViewById(R.id.btn_voice_11_12);

        btn_voice_0_2.setOnClickListener(this);
        btn_voice_3_4.setOnClickListener(this);
        btn_voice_5_6.setOnClickListener(this);
        btn_voice_7_8.setOnClickListener(this);
        btn_voice_9_10.setOnClickListener(this);
        btn_voice_11_12.setOnClickListener(this);

    }



    private ArrayList<String> getMusicData(int id) {
        switch (id) {
            case R.id.btn_voice_0_2:
                mVoiceList.clear();
                mVoiceList.add("睡眠曲");
                mVoiceList.add("唱儿歌");
                mVoiceList.add("去跳舞");
                mVoiceList.add("讲故事");
                mVoiceList.add("动画片");
                mVoiceList.add("好孩子");
                mVoiceList.add("说英语");
                break;
            case R.id.btn_voice_3_4:
                mVoiceList.clear();
                mVoiceList.add("讲故事");
                mVoiceList.add("说英语");
                mVoiceList.add("懂安全");
                mVoiceList.add("背古诗");
                mVoiceList.add("动画片");
                mVoiceList.add("讲外语");
                mVoiceList.add("算算数");
                break;
            case R.id.btn_voice_5_6:
                mVoiceList.clear();
                mVoiceList.add("做作文");
                mVoiceList.add("做英语");
                mVoiceList.add("动画片");
                mVoiceList.add("笑一笑");
                mVoiceList.add("学奥数");
                mVoiceList.add("讲外语");
                mVoiceList.add("成语接龙");
                break;
            case R.id.btn_voice_7_8:
                mVoiceList.clear();
                mVoiceList.add("古典文学");
                mVoiceList.add("我学外语");
                mVoiceList.add("牛顿科学");
                mVoiceList.add("医学常识");
                mVoiceList.add("神奇化学");
                mVoiceList.add("国家地理");
                break;
            case R.id.btn_voice_9_10:
                mVoiceList.clear();
                mVoiceList.add("唱京剧");
                mVoiceList.add("听相声");
                mVoiceList.add("说评书");
                mVoiceList.add("广场舞");
                mVoiceList.add("爱红歌");
                break;
            case R.id.btn_voice_11_12:
                mVoiceList.clear();
                mVoiceList.add("唱京剧");
                mVoiceList.add("听相声");
                mVoiceList.add("说评书");
                mVoiceList.add("广场舞");
                mVoiceList.add("爱红歌");
                break;
        }
        return mVoiceList;
    }
}
